package day25;

import java.io.*;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

//用户系统
public class UserSystem {
    static Scanner sc=new Scanner(System.in);
    //保存当前登录成功的用户信息
    static User userLogin;
    static List<User> list=new ArrayList<>();
    static Map<String,String> map=new HashMap<>();
    static File file=new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\cookie.txt");
    static int sum=1;
    //因为它是最先执行的，而且只走一次，去做反序列化，初始化数据
    static {
        input();
        keepinput();
    }
    public static void main(String[] args) {
        one();

    }
    public static void one(){
        while (true){
            System.out.println("****XXX用户系统****");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.退出系统");
            System.out.println("*****************");
            System.out.print("请输入对应的编号:");
            int n=sc.nextInt();
            switch (n){
                case 1:login();break;
                case 2:register();break;
                case 3:
                    System.out.println("用户退出！");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    private static void login() {
        if (file.exists()){
            System.out.println("本地有缓存，自动登录成功！");
            two();
        }
        System.out.print("请输入账号:");
        String un= sc.next();
        System.out.print("请输入密码:");
        String pw=sc.next();
        for (User u:list){
            if (u.getUsername().equals(un)&&u.getPassword().equals(pw)){
                //二级菜单
                userLogin=u;
                System.out.print("是否保存账号密码(y/n):");
                String s=sc.next();
                if ("y".equals(s)){
                    map.put(u.getUsername(),u.getPassword());
                    keepoutput();
                    two();
                    sum=1;
                    return;
                }else {
                    two();
                    return;
                }
            }else {
                while (sum<3){
                    System.out.println("账号或密码错误，请重新输入!");
                    sum++;
                    login();
                    if (sum==3){
                        System.out.println("三次登录失败，退出系统！");
                        System.exit(0);
                    }
                }
            }
        }
    }

    private static void register() {
        System.out.print("请输入注册账号:");
        String username=sc.next();
        System.out.print("请输入注册密码:");
        String password=sc.next();
        System.out.print("请输入注册年龄:");
        int age=sc.nextInt();
        System.out.print("请输入注册城市:");
        String city=sc.next();
        User u=new User(username,password,age,city);
        list.add(u);
        //序列化保存到本地
        output();
    }

    public static void two(){
        endTime();
        while (true){
            System.out.println("***"+userLogin+"登录成功***");
            System.out.println("**************");
            System.out.println("1.查询用户");
            System.out.println("2.修改密码");
            System.out.println("3.删除用户");
            System.out.println("4.新增用户");
            System.out.println("5.退出登录");
            System.out.println("**************");
            System.out.print("请输入对应的编号:");
            int n=sc.nextInt();
            switch (n){
                case 1:select();break;
                case 2:update();break;
                case 3:delete();break;
                case 4:login();break;
                case 5:return;
                default:
                    System.out.println("输入错误，请重新输入!");
                    break;
            }
        }

    }

    private static void select() {
        System.out.println("账号\t密码\t年龄\t城市");
        for (User u:list){
            System.out.println(u.getUsername()+"\t"+u.getPassword()+
                    "\t"+u.getAge()+"\t"+u.getCity()+"\t");
        }
    }

    private static void update() {
        System.out.print("请输入新密码:");
        String pw= sc.next();
        System.out.print("请再次输入新密码:");
        String newpw=sc.next();
        if (pw.equals(newpw)){
            for (User u:list){
                if (u.getUsername().equals(userLogin)){
                    u.setPasswork(newpw);
                    output();
                    break;
                }
            }
        }else {
            System.out.println("两次输入的密码不一致,修改失败");
        }
    }

    private static void delete() {
        System.out.print("请输入删除的账号:");
        String un=sc.next();
        list.removeIf(amount->amount.getUsername().equals(un));
        output();
    }

    public static void output(){ //反序列化
        try(FileOutputStream fos=new FileOutputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\user.txt");
            ObjectOutputStream oos=new ObjectOutputStream(fos);
        ){
            oos.writeObject(list);
            System.out.println("***集合保存成功***");
            fos.close();
            oos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void endTime(){
        if (file.exists()){
            ScheduledExecutorService ses= null;
            ses = Executors.newScheduledThreadPool(1);
            ses.schedule(()->{
                System.out.println("一分钟到，系统自动清除缓存");
                file.delete();
            },1, TimeUnit.MINUTES);
            ses.shutdown();
        }
    }

    public static void input(){ //序列化
        //文件不存在的，不要自己创建，如果创建了空文件，不能正常序列化
        try(FileInputStream fis=new FileInputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\user.txt");
            ObjectInputStream ois=new ObjectInputStream(fis);
        ){
            list=(List<User>)ois.readObject();
            System.out.println("***初始化集合成功***");
            fis.close();
            ois.close();
        }catch (FileNotFoundException e){
            System.out.println("欢迎你第一次使用！");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static void keepoutput(){ //反序列化
        try(FileOutputStream fos=new FileOutputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\cookie.txt");
            ObjectOutputStream oos=new ObjectOutputStream(fos);
        ){
            oos.writeObject(map);
            System.out.println("***缓存集合保存成功***");
            fos.close();
            oos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void keepinput(){ //序列化
        //文件不存在的，不要自己创建，如果创建了空文件，不能正常序列化
        try(FileInputStream fis=new FileInputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\cookie.txt");
            ObjectInputStream ois=new ObjectInputStream(fis);
        ){
            map=(Map<String, String>)ois.readObject();
            System.out.println("***缓存初始化集合成功***");
            fis.close();
            ois.close();
        }catch (FileNotFoundException e){
            System.out.println("欢迎你第一次使用！");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }

}

class User implements Serializable {
    private String username;
    private String password;
    private int age;
    private String city;

    public User(String username, String password, int age, String city) {
        this.username = username;
        this.password = password;
        this.age = age;
        this.city = city;
    }
    public User(){}

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPasswork(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}
